<?php

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $heading
	 * @param unknown_type $headingRank
	 * @param unknown_type $class
	 * @return unknown
	 */
	function fta_widgetHeadingTag($heading,$headingRank=1,$class="sifr"){
			
		return "<h$headingRank class=\"$class\">$heading</h$headingRank>";
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $heading
	 * @param unknown_type $class
	 * @return unknown
	 */
	function fta_widgetH1Tag($heading,$class="sifr"){
		return fta_widgetHeadingTag($heading,1,$class);
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $heading
	 * @param unknown_type $class
	 * @return unknown
	 */
	function fta_widgetH2Tag($heading,$class="sifr"){
		return fta_widgetHeadingTag($heading,2,$class);
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $heading
	 * @param unknown_type $class
	 * @return unknown
	 */
	function fta_widgetH3Tag($heading,$class="sifr"){
		return fta_widgetHeadingTag($heading,3,$class);
	}		
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $heading
	 * @param unknown_type $class
	 * @return unknown
	 */
	function fta_widgetH4Tag($heading,$class="sifr"){
		return fta_widgetHeadingTag($heading,4,$class);
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $heading
	 * @param unknown_type $class
	 * @return unknown
	 */
	function fta_widgetH5Tag($heading,$class="sifr"){
		return fta_widgetHeadingTag($heading,5,$class);
	}		
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $heading
	 * @param unknown_type $class
	 * @return unknown
	 */
	function fta_widgetH6Tag($heading,$class="sifr"){
		return fta_widgetHeadingTag($heading,6,$class);
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $links
	 * @param unknown_type $listClass
	 * @param unknown_type $altItemClass
	 * @param unknown_type $firstItemClass
	 * @param unknown_type $lastItemClass
	 */
	function fta_widgetNavigationList($links,$listClass="list",$altItemClass='altItem',$firstItemClass='firstItem', $lastItemClass='lastItem'){
		if (!is_array($links))
			return;

		static $renderNavigationListItemFunction;
		if (is_null($renderNavigationListItemFunction)){//candidate for stuffing into apc cache
			$renderNavigationListItemFunction = create_function('$item','echo "<a href=\"$item->destination\">$item->text</a>";');
		}

		fta_widgetList($links,$listClass,$altItemClass,$firstItemClass, $lastItemClass,$renderNavigationListItemFunction);

	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $items
	 * @param unknown_type $listClass
	 * @param unknown_type $altItemClass
	 * @param unknown_type $firstItemClass
	 * @param unknown_type $lastItemClass
	 * @param unknown_type $renderItemFunction
	 */
	function fta_widgetList($items,$listClass="list",$altItemClass='altItem',$firstItemClass='firstItem', $lastItemClass='lastItem',$renderItemFunction) {
?>
<ul class="<?= $listClass ?>">
<?		
	$row = -1;
	$numRows = count($items);
	
	foreach($items as $item){
		
		$itemClasses=array();
		
		if ($altItemClass && 0 == ($row++) % 2 ) 
			array_push($itemClasses,$altItemClass);
		
		if ($firstItemClass && 0 == $row)
			array_push($itemClasses,$firstItemClass);			
			
		if ($lastItemClass && $numRows -1 == $row)
			array_push($itemClasses,$lastItemClass);

?>		
		<li <?= ( 0 < count($itemClasses) ? "class=\"" . implode(" ",$itemClasses) . "\"" : "") ?> ><?= $renderItemFunction( $item ) ?></li>		
<?		
	}
?>
</ul>
<?		
	}


	function fta_widgetStarRating($id, $rating, $outOf, $label = null, $class='rating', $filenamePrefix=''){
		//ob_start();
?>
			<div id="<?= $id ?>" class="<?= $class ?>">

<?
	if ($label){
?><span><?= $label ?></span><?
	}

	for($i=1; $i <= ceil($outOf); $i++){

		if ($rating > 0 && $i <= $rating ) {
			$graphic = '/images/'.$filenamePrefix.'star-on.gif';
		} else if (($rating > $i-1) && ($rating < ($i)) ){
				$graphic = '/images/'.$filenamePrefix.'star-half.gif';
		} else {
			$graphic = '/images/'.$filenamePrefix.'star-off.gif';
		}
?>
				<img src="<?= $graphic ?>" />
<?
	}
?>
            </div>
<?
	}
	
	
	/**
	 * prints out the html for an accordion, it does not set up the javascript, as that is display specific
	 * 
	 * prints an accordion, we pass in a list of items ie array( id = > title)  for the accordion grab handles
	 * and a render function which is used to render the accordion elements
	 */
	
	function fta_widgetAccordion($accordionId, $accordionItems,$accordionItemsRenderFunction,$accordionClass='toggler', $accordionElementClass='element'){
		
		//we dont provide a default render function, as this is 
		// dependant on what we are printing and there is no simple way to get the data..
		//we should throw an error.. but we dont want errors showing up on the site
		if (is_null($accordionItemsRenderFunction))
			return;
			
?>
			<div id="<?= $accordionId ?>">
<?				
		foreach ($accordionItems as $accordionItem)		{
?>
<h3 class="toggler" id="<?= $accordionItem->id ?>"><?= $accordionItem->title ?></h3>
<div class="element"> 
<?= $accordionItemsRenderFunction( $accordionItem );?> 
</div>
<?			
		}
?>
			</div>
<?		
	}
	
	
	function fta_widgetLink($href,$text,$altText,$class,$target) {
?>		
		<a href="<?= $href ?>" <?= (0 <strlen($target)) ? "target=\"$target\"": "" ?> <?= (0 <strlen($class)) ? "class=\"$class\"": "" ?> <?= (0 <strlen($altText)) ? "alt=\"$altText\"": "" ?>><?= $text ?></a>
<?		
	}
	
	
	function  fta_redirectCleanly( $url, $status=302 ) {
		while (@ob_end_clean());//we clean all output buffers..
		header("Location:" . $url , true,$status);				
		die();
	}
	
	function fta_widgetGoogleAnalytics($pageIdentifier=null) {
		//UA-11289965-1
		global $fta_;
		global $FTAGoogleAnalyticsCode;//assume this is always set
		
		if (!array_key_exists('analyticsEmbedded',$fta_) || is_null($FTAGoogleAnalyticsCode) || !$fta_->analyticsEmbedded){
			

			$fta_->analyticsEmbedded = true;
		
?>		
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("<?= $FTAGoogleAnalyticsCode ?>");
<?php
		if(is_null($pageIdentifier)){ 
?>
pageTracker._trackPageview();
<?php 
		} else {
?>
pageTracker._trackPageview('<?= $pageIdentifier ?>');
<?php 
		}
?>
} catch(err) {}
</script>		
<?php
		} 		
	}
	
	function fta_widgetGoogleAnalyticsHeader($pageIdentifier=null) {
		//UA-11289965-1
		global $fta_;
		global $FTAGoogleAnalyticsCode;//assume this is always set
		
		if (!array_key_exists('analyticsEmbedded',$fta_) || is_null($FTAGoogleAnalyticsCode) || !$fta_->analyticsEmbedded){
			

			$fta_->analyticsEmbedded = true;
		
?>	
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '<?= $FTAGoogleAnalyticsCode ?>']);
  <?php
		if(is_null($pageIdentifier)){ 
  ?>
  _gaq.push(['_trackPageview']);
  <?php 
		} else {
  ?>
  _gaq.push(['_trackPageview', '<?= $pageIdentifier ?>']);
  <?php 
		}
  ?>
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<?php
		} 		
	}
	
	function fta_ensurePageUrlIsCanonical($pageIdentifier){
		if ($_SERVER['REQUEST_URI'] != $pageIdentifier){
			header("HTTP/1.1 301 Moved Permanently",true,301);
			header("Location: http://" . $_SERVER['SERVER_NAME'] .$pageIdentifier ,true,301);
		
			echo '<a href="http://' . $_SERVER['SERVER_NAME'] . $pageIdentifier . '" >Moved Here</a>'. "\n\n";
			while(@ob_clean()){};
			die();
		}		
	}
	
	function fta_canonicalPageUrl($pageIdentifier){
		//this assumes that there is a CMS_HTTP_HOST defined
		return '<link rel="canonical" href="'. CMS_HTTP_HOST . $pageIdentifier . '" />';
	}
	
	function fta_doCookiesWork($cookieName='cookieTest'){
		$c = extractValueWithKeyFromArray($cookieName,$_COOKIE,null);
		if (is_null($c)) {
			//cookie not set.. do the test.. 
			
			$cookieSet = extractBooleanWithKeyFromArray('cookieSet',$_REQUEST,false);
			
			if ($cookieSet){
				return false;
			} else {
				$request = $_SERVER['REQUEST_URI'];
				if (strpos($request,'cookieSet') === false) {
					setcookie($cookieName,'1');
					header("location: " . $request . '?cookieSet=1');
				} else {
					return false;
				}				
			}
			
			
		} else {
			//cookie set, return true..
			return true;
		}
	}
	
	function fta_noCache(){
		//set headers here to ensure things are not cached.. 
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT",true); // Date in the pasts
		header("Etag: " . md5("slartybartfast" .  microtime(true) * microtime(true)),true); // Date in the pasts
		header("max-age: 1", true);
		header("s-max-age: 1", true);
		header("Pragma: no-cache", true);
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0", true);
		header("Cache-Control: private",false);	
	}

	function fta_outputClean($string, $allowedTags = null) {
//		
		//Strip tags then htmlentities
		$string = fta_stripTags($string, $allowedTags);
		
		return htmlentities($string, ENT_QUOTES, 'UTF-8', false);
//		return $string;
		
	}
	
	function fta_stripTags($str, $tags = null, $stripContent = false) {
    /*	preg_match_all("/<([^>]+)>/i", $tags, $allTags, PREG_PATTERN_ORDER); 
	    foreach ($allTags[1] as $tag) { 
	        $replace = "%(<$tag.*?>)(.*?)(<\/$tag.*?>)%is"; 
	        $replace2 = "%(<$tag.*?>)%is"; 
	        echo $replace; 
	        if ($stripContent) { 
	            $str = preg_replace($replace,'',$str); 
	            $str = preg_replace($replace2,'',$str); 
	        } 
	            $str = preg_replace($replace,'${2}',$str); 
	            $str = preg_replace($replace2,'${2}',$str); 
	    } 
	    return $str;*/
		return strip_tags($str,$tags); 
	}
	
	function fta_formValueFromArrayOrDefault($name,$array,$default='',$allowedTags=null){
		if (is_a($array, 'FTAObject')&& isset($array->{ $name }) && !is_null($array->{ $name } ) ){
			return $array->{ $name };
		} else if (is_array($array)){
			return fta_outputClean(extractStringWithKeyFromArray($name,$array,$default),$allowedTags);
		} else {
			return $default;
		}
	}
	
	